package com.mmall.controller.backend;

import ch.qos.logback.core.ConsoleAppender;
import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping(value = "/manage/user")
public class UserManageController {

    @Autowired
    private IUserService iUserService;

    /**
     * 管理员登陆
     * @param username
     * @param password
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "login.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<User> login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password, HttpSession httpSession){
//        登陆
        ServerResponse<User> response = iUserService.login(username, password);
//        判断登陆成功
        if (response.isSuccess()){
//            获取查询到的user信息
            User user = response.getData();
//            判断登陆的是否是管理员
            if (user.getRole() == Const.Role.ROLE_ADMIN){
//                如果是管理员，则将其放入session
                httpSession.setAttribute(Const.CURRENT_USER, user);
                return response;
            }else {
                return ServerResponse.createByErrorMessage("不是管理员，无法登陆");
            }
        }
        return response;
    }
}
